/*
 * @Author: askMeWhy
 * @Date:   2018-07-05 09:57:40
 * @Last Modified by: AskMeWhy
 * @Last Modified time: 2021-07-22 14:02:40
 */
import { sessionStorage } from "../utils/storage";
// const url = "http://192.168.10.222:9290/saas-api"; // 测试
// const url = "http://192.168.10.79:9290/saas-api"; // 薛通建
// const url = "http://192.168.1.24:9290/saas-api"; // 章贵敏
// const url = "http://192.168.1.9:9290/saas-api"; // 赵景驰
// const authUrl = "http://192.168.1.9:9290/saas-api";
// const url = "https://app.tianzuoyun.com/api/cms";
const staticUrl = "https://test-1304386495.cos.ap-chengdu.myqcloud.com";
// const staticUrl = "https://tianzuoyun-prod.oss-cn-shenzhen.aliyuncs.com";

// const isDev = process.env.NODE_ENV === "development";
const isDev = true;
export interface ApiModel {
    name?: string;
    url?: string;
    delete?: boolean;
}
export interface ApiDragModel {
    custom?: boolean;
    x?: string | number | undefined;
    y?: string | number | undefined;
}
export interface ApiInfoModel {
    fixed?: boolean;
    list?: ApiModel[];
    current?: ApiModel;
    drag?: ApiDragModel;
}
export interface ApiClassModel {
    apiInfo: ApiInfoModel;
    current: ApiModel;
    fixed: boolean;
    drag: ApiDragModel;
    apiList: ApiModel[];
    configList: ApiModel[];
}
export const apiList: ApiModel[] = [
           { name: "测试", url: "http://192.168.10.222:9290/saas-api" },
           { name: "薛通建", url: "http://192.168.10.79:9290/saas-api" },
           { name: "薛通建new", url: "http://192.168.10.163:9290/saas-api" },
           { name: "章贵敏", url: "http://192.168.1.24:9290/saas-api" },
           { name: "赵景驰", url: "http://192.168.1.9:9290/saas-api" }
];
class ApiInfoExtension implements ApiClassModel {
    constructor() {
        if (!this.current.url) {
            this.apiInfo = {
                ...this.apiInfo,
                current: {
                    ...apiList[0]
                }
            };
        }
    }

    get apiInfo() {
        const apiInfo =
            sessionStorage.getItem<ApiInfoModel|null>("apiInfo") ||
            ({} as ApiInfoModel);
        return apiInfo;
    }

    set apiInfo(val: ApiInfoModel) {
        sessionStorage.setItem("apiInfo", val);
    }

    get current() {
        const apiInfo = this.apiInfo;
        return apiInfo.current || ({} as ApiModel);
    }

    get fixed() {
        const apiInfo = this.apiInfo;
        return !!apiInfo.fixed;
    }

    get drag() {
        const apiInfo = this.apiInfo;
        return apiInfo.drag || ({} as ApiDragModel);
    }

    get apiList() {
        const apiInfo = this.apiInfo;
        return (apiInfo.list || []).map((cur: ApiModel) => {
            return {
                ...cur,
                delete: true
            };
        });
    }

    get configList() {
        return apiList;
    }
}
export const apiInfoExtension: ApiClassModel = isDev
    ? new ApiInfoExtension()
    : {} as ApiClassModel;

export const STATIC_URL =
    isDev
        ? staticUrl
        : process.env.VUE_APP_STATIC_API;
export const API_DEFAULT_CONFIG = {
    BaseURL:
        isDev
            ? apiInfoExtension.current.url
            : process.env.VUE_APP_SERVER_API,
    SocketUrl: "192.168.10.222:9295/api/cms",
    MockURL: ""
};
